//
//  TeacherCheckOn.m
//  iBaby365v2
//
//  Created by Kirinrin on 14/12/19.
//  Copyright (c) 2014年 suplus. All rights reserved.
//

#import "TeacherCheckOnViewController.h"
#import "AppDelegate.h"
#import "NetworkAccessManager.h"
#import "UIAlertView+Kirinrin.h"
#import "ChildSelectCollectionCell.h"
#import <UIImageView+AFNetworking.h>
#import "NSDate+util.h"

@interface TeacherCheckOnViewController()
@property(nonatomic)NSArray *childList;
@property(nonatomic)NSArray *markList;
@property(nonatomic)NSArray *countList;

@property(nonatomic)NSMutableArray *selectedArray;
@end

@implementation TeacherCheckOnViewController
-(void)viewDidLoad{
    [super viewDidLoad];
    [self.collectionView setAllowsMultipleSelection:YES];
    [self loadRemoteData];
}



-(void)loadRemoteData{
    
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    NSString *uriString = [NSString stringWithFormat:@"Child/%@/queryClassChildWithMarkList.json", delegate.user[@"kClassId"]];
    
    NSDictionary *parameters = @{@"key": delegate.accessKey};
   
    [[NetworkAccessManager shareManager] GET:uriString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"opernation: %@\nresponse: %@", operation, responseObject);
        
        int resultCode = [responseObject[@"result"]intValue];
        if (resultCode == 0) {
            
            NSDictionary *data = responseObject[@"data"];
            self.markList = data[@"markList"];
            self.childList = data[@"childList"];
            self.countList = data[@"countList"];
            
            [self.collectionView reloadData];
            for (NSDictionary *markDict in self.markList) {
                [self.childList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                    if ([markDict[@"childId"]isEqualToString:obj[@"id"]]) {
                        [self.collectionView selectItemAtIndexPath:[NSIndexPath indexPathForItem:idx inSection:0] animated:NO scrollPosition:UICollectionViewScrollPositionNone];
                        *stop = YES;
                    }
                }];
            }
        }else{
            [UIAlertView alertWithRemoteError:resultCode];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"opernation: %@\nerror: %@", operation, error);
        [UIAlertView alertWithNetworkError];
    }];
    
}


#pragma mark - UICollectionViewDelegate, UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [self.childList count];
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId = @"Cell";
    ChildSelectCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
    NSDictionary *childDict = self.childList[indexPath.row];
    //    [cell.photo setImage:[UIImage imageNamed:@"imageUserPhotoDefault"]];
    NSLog(@"photo url %@",childDict[@"photo"] );
    [cell.childName setText:childDict[@"name"]];
    if (![childDict[@"photo"] isEqual:[NSNull null]]) {
        NSURL *photoUrl = [NSURL URLWithString:childDict[@"photo"]];
        [cell.childImage setImageWithURL:photoUrl placeholderImage:[UIImage imageNamed:@"imageUserPhotoDefault"]];
    }else{
        [cell.childImage setImage:[UIImage imageNamed:@"imageUserPhotoDefault"]];
    }
    

    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"didSelectItemAtIndexPath");
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    NSString *childId = self.childList[indexPath.row][@"id"];
    NSString *uriString = [NSString stringWithFormat:@"Child/%@/mark.json", childId];
    NSString *classId = self.childList[indexPath.row][@"cid"];
    
    NSDictionary *parameters = @{@"key": delegate.accessKey, @"classId":classId, @"markDate":[[NSDate date]stringWithDateFormate]};
    
    [[NetworkAccessManager shareManager] GET:uriString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"opernation: %@\nresponse: %@", operation, responseObject);
        
        int resultCode = [responseObject[@"result"]intValue];
        if (resultCode == 0) {

        }else{
            [UIAlertView alertWithRemoteError:resultCode];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"opernation: %@\nerror: %@", operation, error);
        [UIAlertView alertWithNetworkError];
    }];
}
-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"didDeselectItemAtIndexPath");
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
   NSString *childId = self.childList[indexPath.row][@"id"];
    NSString *uriString = [NSString stringWithFormat:@"Child/%@/unmark.json", childId];
   NSString *classId = self.childList[indexPath.row][@"cid"];
    NSDictionary *parameters = @{@"key": delegate.accessKey, @"classId":classId, @"markDate":[[NSDate date]stringWithDateFormate]};
    
    [[NetworkAccessManager shareManager] GET:uriString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"opernation: %@\nresponse: %@", operation, responseObject);
        
        int resultCode = [responseObject[@"result"]intValue];
        if (resultCode == 0) {
            
        }else{
            [UIAlertView alertWithRemoteError:resultCode];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"opernation: %@\nerror: %@", operation, error);
        [UIAlertView alertWithNetworkError];
    }];
}

#pragma mark - navigation
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if ([segue.identifier isEqualToString:@"toCheckOnList"]) {
        [segue.destinationViewController setValue:self.childList forKey:@"childArray"];
        [segue.destinationViewController setValue:self.countList forKey:@"countArray"];
    }
}

#pragma mark - getter&setter
-(NSMutableArray *)selectedArray{
    if (!_selectedArray) {
        _selectedArray = [NSMutableArray array];
    }
    return _selectedArray;
}
@end
